/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.wirelesssecuritymodule.services;

import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.wirelesssecuritymodule.core.WirelessSecurityProtocol;
import org.testis.wirelesssecuritymodule.core.WirelessSecurityProtocolFactory;
import org.testis.wirelesssecuritymodule.data.WirelessSecurityModuleDataObject;


/**
 *
 * @author root
 */
public class WirelessSecurityModulePrepareWpaConfAction extends AtofModuleAction
{

    @Override
    public boolean operateRequest(AtofModuleService request)
    {


        WirelessSecurityModuleDataObject dataObj =
            (WirelessSecurityModuleDataObject) request.getDataObj();

        LogSenderService.putMessageOnQueue(
            "WirelessSecurityModulePrepareWpaConfAction",
            AtofLogLevel.INFO,
            "Prepare Wap configuration requested",
            dataObj.getMessageId());
        boolean result = false;
        WirelessSecurityProtocol protocol = WirelessSecurityProtocolFactory.
            getWirelessSecurityProtocol(dataObj);
        if (protocol != null) {
            result = protocol.createProfile();
            if (!result) {
                LogSenderService.putMessageOnQueue(
                    "WirelessSecurityModulePrepareWpaConfAction",
                    AtofLogLevel.ERROR,
                    "Configuration file not created. An exception occured while creating profile file...",
                    dataObj.getMessageId());
            }
            else {
                LogSenderService.putMessageOnQueue(
                    "WirelessSecurityModulePrepareWpaConfAction",
                    AtofLogLevel.INFO,
                    "Configuration file  created...",
                    dataObj.getMessageId());
            }
        }
        else {
            LogSenderService.putMessageOnQueue(
                "WirelessSecurityModulePrepareWpaConfAction",
                AtofLogLevel.ERROR,
                "Given protocol type (" + dataObj.getWpaProto() +
                ") is incorrect or not supported." +
                " Supported protocols: NONE/WPA/WEP/WPA2/CUSTOM",
                dataObj.getMessageId());
        }
        AtofLogLevel level = result ? AtofLogLevel.INFO : AtofLogLevel.ERROR;
        LogSenderService.putMessageOnQueue(
            "WirelessSecurityModulePrepareWpaConfAction",
            level,
            "WirelessSecurityModulePrepareWpaConfAction resulted:" +
            Boolean.valueOf(result),
            dataObj.getMessageId());
        return result;
    }
}
